# SettingsDialog.py
# Copyright (C) otacon 2010 <orfeo18@gmail.com>
# 
# AdSense-Monitor is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# AdSense-Monitor is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import gobject
import os
import sys

from core import Path

try:
	import pygtk
	#pygtk.require("2.17.0")
except:
	print "pygtk not found"
	sys.exit(1)
try:
	import gtk
except:
	print "gtk or glade not found"
	sys.exit(1)

class SettingsDialog(gobject.GObject):
    
    def __init__( self , configuration=None ):
        ''' This class implements the settings dialog '''
        gobject.GObject.__init__(self)
        builder = os.path.join(Path.WORKING_PATH,'gui',"Settings.ui")

        self._ui = gtk.Builder()
        self._ui.add_from_file( builder )

        self.widgets = {}
        self.values = {}
        self.generate_config_bindings()
        self.set_configuration(configuration)
        self._ui.connect_signals(self)

    def generate_config_bindings(self):
        self.widgets['auto_update'] = self._ui.get_object('auto_update')
        self.widgets['update_rate'] = self._ui.get_object('update_rate')
        self._ui.get_object('update_rate').set_value(5.0)

        self.widgets['update_tooltip'] = self._ui.get_object('update_tooltip')
        self.widgets['show_impressions'] = self._ui.get_object('show_impressions')
        self.widgets['show_ecpm'] = self._ui.get_object('show_ecpm')
        self.widgets['show_clicks'] = self._ui.get_object('show_clicks')
        self.widgets['show_ctr'] = self._ui.get_object('show_ctr')
        self.widgets['show_earnings'] = self._ui.get_object('show_earnings')
        self.widgets['show_report_time'] = self._ui.get_object('show_report_time')

        self.widgets['play_sound'] = self._ui.get_object('play_sound')
        self.widgets['sound_file'] = self._ui.get_object('sound_file')
        self.widgets['sound_impressions'] = self._ui.get_object('sound_impressions')
        self.widgets['sound_ecpm'] = self._ui.get_object('sound_ecpm')
        self.widgets['sound_clicks'] = self._ui.get_object('sound_clicks')
        self.widgets['sound_ctr'] = self._ui.get_object('sound_ctr')
        self.widgets['sound_earnings'] = self._ui.get_object('sound_earnings')
        self.widgets['sound_report_time'] = self._ui.get_object('sound_report_time')

        self.widgets['show_popups'] = self._ui.get_object('show_popups')
        self.widgets['notify_impressions'] = self._ui.get_object('notify_impressions')
        self.widgets['notify_ecpm'] = self._ui.get_object('notify_ecpm')
        self.widgets['notify_clicks'] = self._ui.get_object('notify_clicks')
        self.widgets['notify_ctr'] = self._ui.get_object('notify_ctr')
        self.widgets['notify_earnings'] = self._ui.get_object('notify_earnings')
        self.widgets['notify_report_time'] = self._ui.get_object('notify_report_time')

        for alias in self.widgets:
            widget = self.widgets[alias]
            if isinstance(widget,gtk.SpinButton):
                self.values[alias] = widget.get_value()
            elif isinstance(widget,gtk.Entry):
                self.values[alias] = widget.get_text()
            elif isinstance(widget,gtk.CheckButton):
                self.values[alias] = widget.get_active()
            elif isinstance(widget,gtk.FileChooserButton):
                self.values[alias] = widget.get_filename()

    def set_configuration(self, configuration):
        self.configuration = configuration
        for alias in self.widgets:
            value = configuration.get_preference(alias)
            widget = self.widgets[alias]
            if isinstance(widget,gtk.SpinButton):
                widget.set_value(value)
            elif isinstance(widget,gtk.Entry):
                widget.set_text(value)
            elif isinstance(widget,gtk.CheckButton):
                widget.set_active(value)
            elif isinstance(widget,gtk.FileChooserButton):
                widget.set_filename(value)

        snd_file = os.path.join(Path.DATA_PATH,'notification.wav')
        self._ui.get_object('sound_file').set_filename(snd_file)
        self._ui.get_object('update_rate').set_value(5.0)
        return

    def open_dialog(self):
        self._ui.get_object("settings_window").show()

    def get_window(self):
        return self._ui.get_object("settings_window")

    def on_cancel_button_clicked(self,cancel):    
        self._ui.get_object("settings_window").hide()
        self.changed_values = {}

    def on_string_changed(self,data):
        for alias in self.widgets:
            if self.widgets[alias] == data:
                self.values[alias] = data.get_text()
                return

    def on_number_changed(self,data):
        for alias in self.widgets:
            if self.widgets[alias] == data:
                self.values[alias] = data.get_value()
                return

    def on_bool_changed(self,data):
        for alias in self.widgets:
            if self.widgets[alias] == data:
                self.values[alias] = data.get_active()
                return

    def on_file_changed(self,data):
        for alias in self.widgets:
            if self.widgets[alias] == data:
                self.values[alias] = data.get_filename()
                return

    def on_ok_button_clicked(self,data):
        self._ui.get_object("settings_window").hide()
        if self.configuration:
            self.configuration.set_preferences(self.values)

    def on_show_popups_toggled(self, widget):
        frame = self._ui.get_object("popup_frame")
        if widget.get_active():
            frame.show_all()
        else:
            frame.hide()

    def on_update_tooltip_toggled(self, widget):
        frame = self._ui.get_object("tray_frame")
        if widget.get_active():
            frame.show_all()
        else:
            frame.hide()

    def on_play_sound_toggled(self, widget):
        frame = self._ui.get_object("sound_frame")
        if widget.get_active():
            frame.show_all()
        else:
            frame.hide()
